import java.awt.BorderLayout;

public class LookAndFeel extends JFrame implements WindowListener {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test frame = new test();
                    JOptionPane.setRootFrame(frame);
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public LookAndFeel() {
        addWindowListener(this);
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        setTitle("test");
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        
        JButton btnExit = new JButton("Exit");
        btnExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                dispatchEvent(new WindowEvent(JOptionPane.getRootFrame(), WindowEvent.WINDOW_CLOSING));
            }
        });
        contentPane.add(btnExit, BorderLayout.CENTER);
    }
    public void windowActivated(WindowEvent arg0) {
        System.out.println("activated");        
    }

    public void windowClosed(WindowEvent arg0) {
        System.out.println("closed");
        System.exit(0);
    }

    public void windowClosing(WindowEvent arg0) {
        System.out.println("closing");
        int close = JOptionPane.showConfirmDialog(
                JOptionPane.getRootFrame(),
                "確定要結束程式嗎？",
                "警告",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE);
        if (close == JOptionPane.YES_OPTION) {
            dispose();
        }
    }

    public void windowDeactivated(WindowEvent arg0) {
        System.out.println("deactivated");              
    }

    public void windowDeiconified(WindowEvent arg0) {
        System.out.println("deiconified");      
    }

    public void windowIconified(WindowEvent arg0) {
        System.out.println("iconified");        
    }

    public void windowOpened(WindowEvent arg0) {
        System.out.println("opened");       
    }
}
